Page({
  data: {
    name: '',
    gender: '',
    age: '',
    grade: '',
    parentsName: '',
    parentsOccupation: '',
    familyIncome: '',
    major: '',
    gpa: '',
    bankAccount: '',
    bankName: '',
    bankBranch: '',
    remark: '',
    applicationSubmitted: false // 申请提交状态字段
  },

  onLoad: function(options) {
    // 检查是否从page2页面返回，并更新页面数据和申请提交状态
    if (options.fromPage2 === 'true') {
      const applicationData = wx.getStorageSync('applicationData');
      if (applicationData) {
        this.setData(applicationData);
      }
      const applicationSubmitted = wx.getStorageSync('applicationSubmitted');
      if (applicationSubmitted) {
        this.setData({
          applicationSubmitted: true
        });
      }
    }
  },

  bindInput: function(e) {
    const field = e.currentTarget.dataset.field;
    this.setData({
      [field]: e.detail.value
    });
  },

  submitApplication: function() {
    // 检查必填项是否都已填写
    const {
      name,
      gender,
      age,
      grade,
      parentsName,
      parentsOccupation,
      familyIncome,
      major,
      gpa,
      bankAccount,
      bankName,
      bankBranch
    } = this.data;

    if (!name || !gender || !age || !grade || !parentsName || !parentsOccupation || !familyIncome || !major || !gpa || !bankAccount || !bankName || !bankBranch) {
      wx.showToast({
        title: '请填写所有必填项',
        icon: 'none'
      });
      return;
    }

    // 可选：如果需要发送数据到后端，可以在这里添加相应的逻辑

    // 保存申请数据到本地缓存
    wx.setStorageSync('applicationData', this.data);

    // 假设提交成功，更新状态并跳转到page2页面
    wx.setStorageSync('applicationSubmitted', true);
    this.setData({
      applicationSubmitted: true
    });

    wx.navigateTo({
      url: '/pages/page3/page3'
    });
  },

  modifyApplication: function() {
    // 清空或重置必要的数据，并且更新申请状态
    wx.removeStorageSync('applicationData');
    wx.setStorageSync('applicationSubmitted', false);
    this.setData({
      name: '',
      gender: '',
      age: '',
      grade: '',
      parentsName: '',
      parentsOccupation: '',
      familyIncome: '',
      major: '',
      gpa: '',
      bankAccount: '',
      bankName: '',
      bankBranch: '',
      remark: '',
      applicationSubmitted: false
    });

    wx.navigateTo({
      url: '/pages/page2/page2?account='+this.data.account,
    });
  }
});
